package cn.akwangl.rocketmq;

import cn.akwangl.common.Constant;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;

/**
 * rocketMQ 消息生产者.
 *
 * @author 阿坤同学
 * @date 2022/6/17 22:29
 * @since 0.0.6
 */
@Slf4j
public class Producer {

    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer(Constant.PRODUCER_GROUP);
        producer.setNamesrvAddr(Constant.NAMESRV_ADDR);
        producer.start();

        for (int i = 0; i < 5; i++) {
            Message message = new Message(Constant.TOPIC_TEST, "tags-" + i, ("hello world" + i).getBytes(StandardCharsets.UTF_8));
            producer.send(message);
        }
        producer.shutdown();
    }
}
